package com.czl.module_storehouse.adapter;

import android.annotation.SuppressLint;
import android.content.Intent;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.czl.module_base.bean.CompanyBean;
import com.czl.module_storehouse.R;
import com.czl.module_storehouse.activity.supplier.add.SupplierAddActivity;
import com.czl.module_storehouse.event.CompanyBeanEvent;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

/**
 * @author xubin
 */
public class SupplierHomeAdapter extends BaseQuickAdapter<CompanyBean, BaseViewHolder> {

    private boolean isEdit = true;

    public SupplierHomeAdapter(int layoutResId, @Nullable List<CompanyBean> data) {
        super(layoutResId, data);
    }

    public boolean isEdit() {
        return isEdit;
    }

    @SuppressLint("NotifyDataSetChanged")
    public void setEdit(boolean edit) {
        isEdit = edit;
        notifyDataSetChanged();
    }

    @Override
    protected void convert(@NonNull BaseViewHolder helper, CompanyBean item) {
        helper.setText(R.id.tv_add_name, item.getCompanyName())
                .setText(R.id.tv_user, item.getContactName())
                .setText(R.id.tv_phone, item.getContactPhone())
                .setText(R.id.tv_address, item.getCompanyAddress())
                .setGone(R.id.iv_delete, !isEdit)
                .setGone(R.id.iv_edit, isEdit);
        helper.getView(R.id.iv_edit)
                .setOnClickListener(v -> {
                    //编辑
                    CompanyBeanEvent event = new CompanyBeanEvent();
                    item.setEditable(true);
                    event.setCompanyBean(item);
                    EventBus.getDefault().postSticky(event);
                    getContext().startActivity(new Intent(getContext(), SupplierAddActivity.class));
                });
        helper.getView(R.id.iv_delete)
                .setOnClickListener(v -> {
                    if (getOnItemChildClickListener() != null) {
                        getOnItemChildClickListener().onItemChildClick(SupplierHomeAdapter.this, v, helper.getAbsoluteAdapterPosition());
                    }
                });
    }
}
